package com.qqz.common.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jaeger.library.StatusBarUtil;
import com.qqz.common.utils.DoubleClickHelper;
import com.qqz.common.utils.activity.ActivityManager;

/**
 * @author - qqz
 * @date - 2021-12-16/11:02
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */
public class BaseActivity extends AppCompatActivity {

    public BaseActivity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusBarUtil.setTranslucentForImageViewInFragment(this, 0, null);
        } else {
            StatusBarUtil.setTranslucentForImageViewInFragment(this, 55, null);
        }

        mActivity = this;
    }

    public BaseActivity getContext() {

        return mActivity;
    }

    @Override
    public void onBackPressed() {
        if (!DoubleClickHelper.isOnDoubleClick()) {
            Toast.makeText(mActivity, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            return;
        }
        moveTaskToBack(false);
        new Handler().postDelayed(() -> {
            ActivityManager.getInstance().finishAllActivities();
        }, 300);
    }


    /**
     * 启动Activity(扩展)
     *
     * @param activityClass Activity的class
     */
    public void startActivityEx(Class<?> activityClass) {
        startActivityEx(activityClass, null);
    }



    /**
     * 启动Activity(扩展)
     *
     * @param activityClass Activity的class
     * @param data 数据
     */
    public void startActivityEx(Class<?> activityClass, Bundle data) {
        startActivityEx(this, activityClass, data, 0);
    }

    /**
     * 启动Activity(扩展)
     *
     * @param context 上下文
     * @param activityClass Activity的class
     * @param data 参数
     * @param intentFlags intent标识
     */
    public static void startActivityEx(Context context, Class<?> activityClass, Bundle data, int intentFlags) {
            Intent intent = new Intent(context, activityClass);
            intent.setFlags(intentFlags);
            if (data != null) {
                intent.putExtras(data);
            }
            context.startActivity(intent);
    }
}
